package com.lenovo.smarttraffic.ui.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.zjh.Bean.Bean_ETCAdd;
import com.lenovo.smarttraffic.zjh.Bean_Json.Bean_JsonETCAdd;
import com.lenovo.smarttraffic.zjh.api.Interface_ETCAdd;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.lenovo.smarttraffic.zjh.Setting_Activity.IP;
import static com.lenovo.smarttraffic.zjh.Setting_Activity.URL_My;


public class FirstFragment extends BaseFragment {
    private Spinner spinner;
    private Button btn50;
    private Button btn100;
    private Button btn200;
    private EditText editAdd;
    private Button btnset;
    private int Id = 1;
    private int Money;
    public static List<ETCHistory> list_history = new ArrayList<>();
    SharedPreferences sp;
    SharedPreferences.Editor editor;


    @Override
    protected View getSuccessView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout._zjh_fg1_etcadd, null, false);
        initView(view);
        sp = mContext.getSharedPreferences("History", Context.MODE_PRIVATE);
        editor = sp.edit();
        String json = sp.getString("History", null);
        if (json != null) {
            Type type = new TypeToken<List<ETCHistory>>() {
            }.getType();
            list_history = new Gson().fromJson(json, type);
        }

        return view;
    }

    private void initView(View view) {
        spinner = (Spinner) view.findViewById(R.id.spinner);
        btn50 = (Button) view.findViewById(R.id.btn_50);
        btn100 = (Button) view.findViewById(R.id.btn_100);
        btn200 = (Button) view.findViewById(R.id.btn_200);
        editAdd = (EditText) view.findViewById(R.id.edit_Add);
        btnset = (Button) view.findViewById(R.id.btnset);

        btn50.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn50.setBackgroundColor(Color.BLUE);
                btn50.setTextColor(0xFFFFFFFF);
                btn100.setBackgroundColor(0xFFFFFFFF);
                btn100.setTextColor(0xFF000000);
                btn200.setBackgroundColor(0xFFFFFFFF);
                btn200.setTextColor(0xFF000000);
                CharSequence s = "50";
                editAdd.setText(s);
            }
        });

        btn100.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn100.setBackgroundColor(Color.BLUE);
                btn100.setTextColor(0xFFFFFFFF);
                btn50.setBackgroundColor(0xFFFFFFFF);
                btn50.setTextColor(0xFF000000);
                btn200.setBackgroundColor(0xFFFFFFFF);
                btn200.setTextColor(0xFF000000);
                CharSequence s = "100";
                editAdd.setText(s);
            }
        });

        btn200.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn200.setBackgroundColor(Color.BLUE);
                btn200.setTextColor(0xFFFFFFFF);
                btn100.setBackgroundColor(0xFFFFFFFF);
                btn100.setTextColor(0xFF000000);
                btn50.setBackgroundColor(0xFFFFFFFF);
                btn50.setTextColor(0xFF000000);
                CharSequence s = "200";
                editAdd.setText(s);
            }
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Id = position + 1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        editAdd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {

                } else {
                    if (Integer.parseInt(editAdd.getText().toString()) > 999 || Integer.parseInt(editAdd.getText().toString()) < 1) {
                        s = "";
                        editAdd.setText(s);
                        Toast.makeText(mActivity, "值超限！", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btnset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editAdd.getText().toString().isEmpty()) {
                    Toast.makeText(mActivity, "请输入充值额度！", Toast.LENGTH_SHORT).show();
                } else {
                    Money = Integer.parseInt(editAdd.getText().toString());
                    Log.e("11111111", "onClick: " + URL_My);
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://192.168.1.110:8088/transportservice/action/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    Interface_ETCAdd request = retrofit.create(Interface_ETCAdd.class);
                    Call<Bean_ETCAdd> call = request.getCall(new Bean_JsonETCAdd(Id, Money, "user1"));
                    call.enqueue(new Callback<Bean_ETCAdd>() {
                        @Override
                        public void onResponse(Call<Bean_ETCAdd> call, Response<Bean_ETCAdd> response) {
                            if (response.code() == 200) {
                                if (response.body().getERRMSG().equals("成功")) {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                                    builder.setTitle("提示：");
                                    builder.setMessage("充值成功！");
                                    builder.setCancelable(false);
                                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });
                                    builder.show();
                                    list_history.add(new ETCHistory(Id, Money, getTime1()));
                                    editor.putString("History", new Gson().toJson(list_history)).commit();
                                } else {
                                    Toast.makeText(mContext, "充值失败！没有该车辆信息！", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(mContext, "充值失败！没有该车辆信息！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Bean_ETCAdd> call, Throwable t) {
                            Toast.makeText(mContext, "充值失败！没有该车辆信息！", Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        });

    }

    public String getTime1() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(date);
    }

    public class ETCHistory {
        int id, money;
        String time;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getMoney() {
            return money;
        }

        public void setMoney(int money) {
            this.money = money;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public ETCHistory(int id, int money, String time) {
            this.id = id;
            this.money = money;
            this.time = time;
        }
    }

    @Override
    protected Object requestData() {
//        SystemClock.sleep(1000);/*模拟请求服务器的延时过程*/
        return "";/*加载成功*/
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 类似于 Activity的 onNewIntent()
     */
    @Override
    public void onNewBundle(Bundle args) {
        super.onNewBundle(args);

        Toast.makeText(_mActivity, args.getString("from"), Toast.LENGTH_SHORT).show();
    }
}
